﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuickApp.Core;
using EfSearchModel.Model;
using System.Data.Entity.Validation;
using QuickApp.MVCControl.Tree;
using Resto.CostControl.Core.Models.SysSettings;
using QuickApp.MVCControl;
using Resto.CostControl.Core;
using System.Globalization;
using QuickApp.MVCControl.Form;
using Resto.CostControl.Core.Models;


namespace Resto.CostControl.Web.Controllers
{
    public class MaterialProcessPlanController : Controller
    {
        private readonly IRepository<MaterialProcessPlan> _RepoItem;
        private readonly IUnitOfWork _UnitOfWork;
        private readonly IRepository<OrderDateAndCategory> _OrderDC;
        private readonly IRepository<MaterialProcess> _MaPr;
        private readonly IRepository<MaterialProcessItem> _MaPrItem;
        private readonly IRepository<Material> _Ma;


        public MaterialProcessPlanController(IRepository<MaterialProcessPlan> repoItem, UnitOfWork unitOfWork, IRepository<OrderDateAndCategory> OrderDC, IRepository<MaterialProcess> MaPr, IRepository<MaterialProcessItem> MaPrItem, IRepository<Material> Ma)
        {
            _RepoItem = repoItem;
            _UnitOfWork = unitOfWork;
            _OrderDC = OrderDC;
            _MaPr = MaPr;
            _MaPrItem = MaPrItem;
            _Ma = Ma;
        }

        [HttpPost]
        public JsonResult List(DWZPageInfo pager, DWZQueryModel where)
        {
            // 分页查询数据
            var list = _RepoItem.GetPageable(pager.ToPageInfo(), where);
            var pageList = list.Page.ToList();
            foreach (var item in pageList)
            {
                item.TotalWeight = item.TotalWeight / 500;
                item.PurchaseWeight = item.PurchaseWeight / 500;
                item.ActualSorage = item.ActualSorage / 500;
                item.SafeStorage = item.SafeStorage / 500;
            }
            var fdata = pager.ToFlexiGridData<MaterialProcessPlan>(pageList, list.Total);

            return Json(fdata);
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult PlanList()
        {
            var types = (from plan in _RepoItem.FindAll()
                         select new ProcessTypeViewModel
                         {
                             Category = plan.Category,
                             Date = plan.Date,
                         }).Distinct().ToList();

            foreach (var type in types)
            {
                type.CategoryName = DependencyResolver.Current.GetService<IEnumService>().GetEnumText("MaterialCategory", type.Category);
            }
            return View(types);
        }

        public ActionResult PlanEdit(DateTime Date, string Category)
        {
            MaterialProcess data = new MaterialProcess();
            data.Id = Guid.NewGuid().ToString();
            data.ProcessDate = Date;
            data.Category = Category;
            data.OrderState = "caogao";
            ViewBag.Category = Category;
            ViewBag.Date = Date;
            return View(data);

        }

        [HttpPost]
        public JsonResult Save(MaterialProcess data, IList<MaterialProcessItem> MaterialList)
        {
            JsonReturnMessages msg = new JsonReturnMessages();
            if (ModelState.IsValid)
            {
                try
                {
                    var ItemDesc = "";
                    if (MaterialList != null)
                    {
                        _MaPrItem.DeleteAll(m => m.OrderId == data.Id);

                        foreach (var m in MaterialList)
                        {
                            m.OrderId = data.Id;
                            m.MaterialId = m.MaterialId;
                            m.Id = Guid.NewGuid().ToString();
                            m.State = "caogao";
                            _MaPrItem.Insert(m);
                            ItemDesc += m.Name + ",";
                        }
                    }

                    if (_MaPr.Find(data.Id) == null)
                    {
                        _MaPr.Insert(data);
                        data.OrderState = "caogao";
                        data.ItemDesc = ItemDesc.Trim(',');
                    }
                    else
                    {
                        data = _MaPr.Find(data.Id);
                        data.OrderState = "caogao";
                        data.ItemDesc = ItemDesc.Trim(',');
                        TryUpdateModel(data);
                    }

                    _UnitOfWork.Commit();
                    msg.IsSuccess = true;
                    msg.Msg = "操作成功";
                }
                catch (Exception ex)
                {
                    msg.IsSuccess = false;
                    msg.Msg = "操作失败：" + ex.Message;
                }
            }

            return Json(msg);
        }

        [HttpPost]
        public JsonResult MaterialList(DWZPageInfo pageInfo, DWZQueryModel where)
        {
            var list = _MaPrItem.GetPageable(pageInfo.ToPageInfo(), where);
            var fdata = pageInfo.ToFlexiGridData<MaterialProcessItem>(list.Page.ToList(), list.Total);
            return Json(fdata);
        }

        public ActionResult MaterialEditList()
        {
            return View();
        }

        [HttpPost]
        public JsonResult MaterialEditList(string parentId, string Category, DateTime? date, DWZPageInfo pageInfo, DWZQueryModel where)//
        {
            if (!string.IsNullOrEmpty(Category))
            {
                where.AddItem("Category", Category);
                if (date != null)
                {
                    where.AddItem("Date", date.ToString(), QueryMethod.DateBlock);
                }
                var list = _RepoItem.GetPageable(pageInfo.ToPageInfo(), where);
                var fdata = pageInfo.ToFlexiGridData<MaterialProcessPlan>(list.Page.ToList(), list.Total);
                return Json(fdata);
            }
            else
            {
                where.AddItem("OrderId", parentId == "" ? null : parentId);
                var list = _MaPrItem.GetPageable(pageInfo.ToPageInfo(), where);
                var fdata = pageInfo.ToFlexiGridData<MaterialProcessItem>(list.Page.ToList(), list.Total);
                return Json(fdata);
            }
        }

        [HttpPost]
        public JsonResult DeleteItem(string Id)
        {
            JsonReturnMessages msg = new JsonReturnMessages();
            if (!string.IsNullOrEmpty(Id))
            {
                try
                {
                    _Ma.Delete(Id);
                    //_UnitOfWork.Commit();

                }
                catch
                {
                    msg.IsSuccess = false;
                    msg.Msg = "操作失败,请稍后重试！";
                }
            }
            else
            {
                msg.IsSuccess = false;
                msg.Msg = "参数错误";
            }
            return Json(msg);
        }
    }

    public class ProcessTypeViewModel
    {
        public string Category { get; set; }
        public string CategoryName { get; set; }
        public DateTime Date { get; set; }
        public string DateString { get { return Date.ToString("yyyy-MM-dd"); } }
    }
}
